package cn.iocoder.yudao.module.project.service.workstation;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.project.controller.admin.workstation.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.workstation.WorkstationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.workstation.WorkstationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 工位 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class WorkstationServiceImpl implements WorkstationService {

    @Resource
    private WorkstationMapper workstationMapper;

    @Resource
    private DictDataApi dictDataApi;

    @Override
    public Integer createWorkstation(WorkstationSaveReqVO createReqVO) {

        List<WorkstationDO>  list = workstationMapper.selectList(WorkstationDO::getProcessOrder, createReqVO.getProcessOrder());
        if(list != null && list.size() > 0){
            //ErrorCode errorCode = new ErrorCode(1001015001, "工艺顺序已存在");
            //throw exception(errorCode);
        }


        list = workstationMapper.selectList(WorkstationDO::getElectronicScaleId, createReqVO.getElectronicScaleId());
        if(list != null && list.size() > 0){
            throw exception(WORKSTATION_ELECTRONIC_SCALE_USED);
        }


        // 插入
        WorkstationDO workstation = BeanUtils.toBean(createReqVO, WorkstationDO.class);
        workstationMapper.insert(workstation);
        // 返回
        return workstation.getId();
    }

    @Override
    public void updateWorkstation(WorkstationSaveReqVO updateReqVO) {
        // 校验存在
        validateWorkstationExists(updateReqVO.getId());

        List<WorkstationDO>  list = workstationMapper.selectList(new QueryWrapper<WorkstationDO>().eq("process_order", updateReqVO.getProcessOrder()).ne("id", updateReqVO.getId()));
        if(Objects.nonNull(list) && list.size() > 0){
            //list = workstationMapper.selectList(new QueryWrapper<WorkstationDO>().ne("id", updateReqVO.getId()));
            //List<Integer> processOrderList = list.stream().map(item -> item.getProcessOrder()).sorted().collect(Collectors.toList());
            //ErrorCode errorCode = new ErrorCode(1001015001, "工艺顺序已存在" + ", 已存在的工艺顺序:" + processOrderList);
            //throw exception(errorCode);
        }

        list = workstationMapper.selectList(new QueryWrapper<WorkstationDO>().eq("electronic_scale_id", updateReqVO.getElectronicScaleId()).ne("id", updateReqVO.getId()));
        if(Objects.nonNull(list) && list.size() > 0){
            throw exception(WORKSTATION_ELECTRONIC_SCALE_USED);
        }

        // 更新
        WorkstationDO updateObj = BeanUtils.toBean(updateReqVO, WorkstationDO.class);
        workstationMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorkstation(Integer id) {
        // 校验存在
        validateWorkstationExists(id);
        // 删除
        workstationMapper.deleteById(id);
    }

    private void validateWorkstationExists(Integer id) {
        if (workstationMapper.selectById(id) == null) {
            throw exception(WORKSTATION_NOT_EXISTS);
        }
    }

    @Override
    public WorkstationDO getWorkstation(Integer id) {
        return workstationMapper.selectById(id);
    }

    @Override
    public PageResult<WorkstationDO> getWorkstationPage(WorkstationPageReqVO pageReqVO) {
        return workstationMapper.selectPage(pageReqVO);
    }

    @Override
    public DictDataRespDTO getWorkstationDept(Integer deptId){
        DictDataRespDTO dictDataRespDTO = dictDataApi.getDictData("workstation_dept", String.valueOf(deptId));
        return dictDataRespDTO;
    }

    @Override
    public DictDataRespDTO getWorkstationDept(String deptName){
        DictDataRespDTO dictDataRespDTO = dictDataApi.parseDictData("workstation_dept", deptName);
        return dictDataRespDTO;
    }


}